<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 13] 13.4 Modifiers</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 16:01:01 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 13<br>Java Syntax</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch13_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-13-SECT-4">13.4 Modifiers</A></h2>

<P CLASS=para>
<A NAME="CH13.MODIFIERS-LI1"></A><A NAME="CH13.VISIBILITY.M1"></A>There are quite a few Java keywords that serve as modifiers
for Java classes, interfaces, methods, and fields.
They are described in <A HREF="ch13_04.htm#JNUT2-CH-13-TAB-4">Table 13.4</A>.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JNUT2-CH-13-TAB-4">Table 13.4: Java Modifiers</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">Modifier</TH>
<TH ALIGN="left">Used On</TH>
<TH ALIGN="left">Meaning</TH>
</TR>
<TR CLASS=row>
<TD ROWSPAN=3 ALIGN="left">abstract</TD>
<TD ALIGN="left">class</TD>
<TD ALIGN="left">

<P CLASS=para>
The class contains unimplemented methods and cannot be
instantiated.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">interface</TD>
<TD ALIGN="left">

<P CLASS=para>
All interfaces are <tt CLASS=literal>abstract</tt>.  The modifier is
optional in interface declarations.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">method</TD>
<TD ALIGN="left">

<P CLASS=para>
No body is provided for the method (it is provided by a
subclass).  The signature is followed by a semicolon.  The
enclosing class must also be <tt CLASS=literal>abstract</tt>.</TD>
</TR>
<TR CLASS=row>
<TD ROWSPAN=4 ALIGN="left">final</TD>
<TD ALIGN="left">class</TD>
<TD ALIGN="left">

<P CLASS=para>
The class may not be subclassed.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">field</TD>
<TD ALIGN="left">

<P CLASS=para>
The field may not have its value changed (compiler may
precompute expressions).</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">method</TD>
<TD ALIGN="left">

<P CLASS=para>
The method may not be overridden (compiler may optimize).</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">variable</TD>
<TD ALIGN="left">

<P CLASS=para>
Java 1.1 and later: the local variable or method or
exception parameter may not have its value changed.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">native</TD>
<TD ALIGN="left">method</TD>
<TD ALIGN="left">

<P CLASS=para>
The method is implemented in C, or in some other
platform-dependent way.  No body is provided; the signature
is followed by a semicolon.</TD>
</TR>
<TR CLASS=row>
<TD ROWSPAN=3 ALIGN="left"><I CLASS=emphasis>none (package)</I></TD>
<TD ALIGN="left">class</TD>
<TD ALIGN="left">

<P CLASS=para>
A non-<tt CLASS=literal>public</tt> class is accessible only in its package</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">interface</TD>
<TD ALIGN="left">

<P CLASS=para>
A non-<tt CLASS=literal>public</tt> interface is accessible only in its package</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">member</TD>
<TD ALIGN="left">

<P CLASS=para>
A member that is not <tt CLASS=literal>private</tt>, <tt CLASS=literal>protected</tt>, or
<tt CLASS=literal>public</tt> has package visiblity and is accessible only
within its package.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">private</TD>
<TD ALIGN="left">member</TD>
<TD ALIGN="left">

<P CLASS=para>
The member is only accessible within the class that defines it.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">protected</TD>
<TD ALIGN="left">member</TD>
<TD ALIGN="left">

<P CLASS=para>
The member is only accessible within the package in which it
is defined, and within subclasses.</TD>
</TR>
<TR CLASS=row>
<TD ROWSPAN=3 ALIGN="left">public</TD>
<TD ALIGN="left">class</TD>
<TD ALIGN="left">

<P CLASS=para>
The class is accessible anywhere its package is.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">interface</TD>
<TD ALIGN="left">

<P CLASS=para>
The interface is accessible anywhere its package is.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">member</TD>
<TD ALIGN="left">

<P CLASS=para>
The member is accessible anywhere its class is.</TD>
</TR>
<TR CLASS=row>
<TD ROWSPAN=4 ALIGN="left">static</TD>
<TD ALIGN="left">class</TD>
<TD ALIGN="left">

<P CLASS=para>
In Java 1.1, a class delared <tt CLASS=literal>static</tt> is a toplevel
class, not an inner class.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">field</TD>
<TD ALIGN="left">

<P CLASS=para>
A <tt CLASS=literal>static</tt> field is a "class field." There is only one
instance of the field, regardless of the number of class
instances created.  It may be accessed through the class name.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">initializer</TD>
<TD ALIGN="left">

<P CLASS=para>
The intializer is run when the class is loaded, rather than
when an instance is created.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">method</TD>
<TD ALIGN="left">

<P CLASS=para>
A <tt CLASS=literal>static</tt> method is a "class method."  It is not passed as an
implicit <tt CLASS=literal>this</tt> object reference.  It may be invoked through the
class name.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">synchronized</TD>
<TD ALIGN="left">method</TD>
<TD ALIGN="left">

<P CLASS=para>
The method makes non-atomic modifications to the class or
instance, and care must be taken to ensure that two threads
cannot modify the class or instance at the same time.  For a
<tt CLASS=literal>static</tt> method, a lock for the class is acquired
before executing the method.  For a non-<tt CLASS=literal>static</tt>
method, a lock for the specific object instance is acquired.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">transient</TD>
<TD ALIGN="left">field</TD>
<TD ALIGN="left">

<P CLASS=para>
The field is not part of the persistent state of the
object, and should not be serialized with the object.</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">volatile</TD>
<TD ALIGN="left">field</TD>
<TD ALIGN="left">

<P CLASS=para>
The field may be accessed by unsynchronized threads, so certain code
optimizations must not be performed on it.</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
<A HREF="ch13_04.htm#JNUT2-CH-13-TAB-5">Table 13.5</A>
summarizes the visibility modifiers; it shows the
circumstances under which class members of the various
visibility types are accessible.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JNUT2-CH-13-TAB-5">Table 13.5: Class Member Accessibility</A></CAPTION>
<TR CLASS=row>
<TH ROWSPAN=2 ALIGN="left">Accessible to:</TH>
<TH COLSPAN=4 ALIGN="center">Member Visibility</TH>
</TR>
<TR CLASS=row>
<TH ALIGN="left">public</TH>
<TH ALIGN="left">protected</TH>
<TH ALIGN="left">package</TH>
<TH ALIGN="left">private</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">Same class</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">Class in same package</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">no</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">Subclass in different package</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">no</TD>
<TD ALIGN="left">no</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">Non-subclass, different package</TD>
<TD ALIGN="left">yes</TD>
<TD ALIGN="left">no</TD>
<TD ALIGN="left">no</TD>
<TD ALIGN="left">no</TD>
</TR>
</TABLE>
<P>
</DIV>
</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch13_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Reserved Words</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
